﻿using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SouthApps.MobileHealth.SDK.DAL.Model;

namespace SouthApps.MobileHealth.SDK.DAL.DataAccess
{
    public class ConvenioDao : DataAccessObject
    {
        #region Add

        public void AddConvenio(Convenio convenio)
        {
            try
            {
                using (var entities = this.EntityContext)
                {
                    entities.AddToconvenio(convenio);
                    
                    this.SaveChanges(entities);
                }
            }
            catch (Exception ex)
            {
                throw this.HandleException(ex);
            }
        }

        #endregion

        #region Modify

        public void ModifyConvenio(Convenio convenio)
        {
            try
            {
                using (var entities = this.EntityContext)
                {
                    Convenio oldConvenio = entities.convenio.FirstOrDefault(e => e.convenioId == convenio.convenioId);
                    oldConvenio = convenio;

                    this.SaveChanges(entities);
                }
            }
            catch (Exception ex)
            {
                throw this.HandleException(ex);
            }
        }

        #endregion

        #region Get

        public List<Convenio> GetConvenios(int financiadorId, int? planId, int? especialidadId, int? sucursalPrestadorId)
        {
            try
            {
                using (var entities = this.EntityContext)
                {
                    var query = from e in entities.convenio
                                where (e.financiadorId == financiadorId && (planId==null || e.planId == planId) && (especialidadId==null || e.especialidadId == especialidadId)
                                && (sucursalPrestadorId==null || e.sucursalPrestadorId == sucursalPrestadorId))
                                select e;

                    return query.ToList();
                }
            }
            catch (Exception ex)
            {
                throw this.HandleException(ex);
            }
        }

        #endregion
    }
}
